package com.qfedu.yanglao20admin.sysAdmin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.constants.CommonConstants;
import com.qfedu.yanglao20.common.exeception.CommonException;
import com.qfedu.yanglao20.entity.sysAdmin.*;
import com.qfedu.yanglao20admin.sysAdmin.dao.SysUserDao;
import com.qfedu.yanglao20admin.sysAdmin.param.SysUserParam;
import com.qfedu.yanglao20admin.sysAdmin.service.SysUserService;
import com.qfedu.yanglao20admin.sysAdmin.vo.SysUserPasswordVO;
import com.qfedu.yanglao20admin.sysAdmin.vo.SysUserVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserDao sysUserDao;

    @Override
    public List<SysUserVO> selectListAll() {
        return sysUserDao.selectListAll();
    }

    @Override
    public void deleteById(Integer id) {
        sysUserDao.deleteById(id);
    }

    @Override
    public Page<SysUserVO> pageSysUserList(SysUserParam param) {
        // 计算偏移量
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        //分页查询
        Page<SysUserVO> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<SysUserVO> sysUserVOS = sysUserDao.selectList(param);

        //页码
        page.setCurrent(param.getPageNum());
        //每页查询的记录数
        page.setSize(param.getPageSize());
        //总记录数
        Long aLong = sysUserDao.selectCount(null);
        page.setTotal(aLong);
        page.setRecords(sysUserVOS);
        return page;
    }

    @Override
    public void add(SysUserVO sysUserVO) {
        if (sysUserVO.getUsername() == null || sysUserVO.getUsername().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写用户名");
        }
        if (sysUserVO.getCompanyName() == null || sysUserVO.getCompanyName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写企业名");
        }
        if (sysUserVO.getAreaName() == null || sysUserVO.getAreaName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写区域名");
        }
        if (sysUserVO.getDepartmentName() == null || sysUserVO.getDepartmentName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写部门名");
        }
        if (sysUserVO.getRoleName() == null || sysUserVO.getRoleName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写角色名称");
        }

        Area area = sysUserDao.selectAreaByName(sysUserVO.getAreaName());
        Company company = sysUserDao.selectCompanyByName(sysUserVO.getCompanyName());
        Department department = sysUserDao.selectDepartmentByName(sysUserVO.getDepartmentName());
        SysRole sysRole = sysUserDao.selectSusRoleByName(sysUserVO.getRoleName());
        Integer areaName = area.getId();
        Integer companyName = company.getId();
        Integer departmentName = department.getId();
        Integer sysRoleName = sysRole.getId();

        SysUser s = new SysUser();
        s.setUsername(sysUserVO.getUsername());
        s.setRealName(sysUserVO.getRealName());
        s.setPhone(sysUserVO.getPhone());
        s.setCompanyId(companyName);
        s.setDepartmentId(departmentName);
        s.setAreaId(areaName);
        s.setRoleId(sysRoleName);
        s.setSort(sysUserVO.getSort());
        s.setEnableFlag(sysUserVO.getEnableFlag());

        sysUserDao.insert(s);
    }

    @Override
    public SysUserVO selectById(Integer id) {
        return sysUserDao.selectById(id);
    }

    @Override
    public void update(SysUserVO sysUserVO) {
        if (sysUserVO.getUsername() == null || sysUserVO.getUsername().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写用户名");
        }
        if (sysUserVO.getCompanyName() == null || sysUserVO.getCompanyName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写企业名");
        }
        if (sysUserVO.getAreaName() == null || sysUserVO.getAreaName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写区域名");
        }
        if (sysUserVO.getDepartmentName() == null || sysUserVO.getDepartmentName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写部门名");
        }
        if (sysUserVO.getRoleName() == null || sysUserVO.getRoleName().isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请填写角色名称");
        }

        Area area = sysUserDao.selectAreaByName(sysUserVO.getAreaName());
        Company company = sysUserDao.selectCompanyByName(sysUserVO.getCompanyName());
        Department department = sysUserDao.selectDepartmentByName(sysUserVO.getDepartmentName());
        SysRole sysRole = sysUserDao.selectSusRoleByName(sysUserVO.getRoleName());
        Integer areaName = area.getId();
        Integer companyName = company.getId();
        Integer departmentName = department.getId();
        Integer sysRoleName = sysRole.getId();

        SysUser s = new SysUser();
        s.setId(sysUserVO.getId());
        s.setUsername(sysUserVO.getUsername());
        s.setRealName(sysUserVO.getRealName());
        s.setPhone(sysUserVO.getPhone());
        s.setCompanyId(companyName);
        s.setDepartmentId(departmentName);
        s.setAreaId(areaName);
        s.setRoleId(sysRoleName);
        s.setSort(sysUserVO.getSort());
        s.setEnableFlag(sysUserVO.getEnableFlag());

        sysUserDao.updateById(s);
    }

    @Override
    public void updatePassword(SysUserPasswordVO passwordVO) {
        String password = "123456";
        SysUserPasswordVO s = new SysUserPasswordVO();
        s.setId(passwordVO.getId());
        s.setPassword(password);
        sysUserDao.updatePassword(s);
    }
}
